package face.sdk;

import face.entity.FaceModel;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * Created by mazhi on 2017/10/22.
 */
public class FaceAPI {

    //创建引擎
    private native long createEngine();

    //检测人脸
    private native FaceModel[] detectFaces(long hander, byte[] imgBgr24, int width, int height,
                                   int step, int maxFaceCount);
    //提取人脸
    private native byte[] extractFeature(long hander, byte[] imgBgr24, int width, int height, int step,FaceModel faceModel);

    //人脸比对
    private native float compare2Feature(long faceEngine, byte[] feature1, byte[] feature2);

    //人脸比对V2
    private native float compare2FeatureV2(long faceEngine, byte[] feature1, byte[] feature2);


    private native void setThreadsNum(int num);

   public FaceAPI(){}

//    static String JNI_LIB_PATH = "/home/elijah/tomcat8.0/lib/libjnifacesdkv3.so";
//    static String JNI_LIB="jnifacesdkv3";
//    static {
////        System.load(JNI_LIB_PATH);
//        String os = System.getProperty("os.name");
//        if(os.toLowerCase().startsWith("win"))
//        {
//            System.loadLibrary("SFaceMatcherJNI");
//        }
//        else
//        {
//            System.loadLibrary("SFaceMatcherSDK");
//        }
//
//    }

    protected long faceEngine;

    /*
    * 初始化SDK
    * */
    public boolean init() {
        faceEngine = createEngine();
        if (faceEngine>0)
            return true;
        return false;
    }

    /*
    * 检测人脸
    *  maxFaceCount最多检测的人脸个数
    *  返回为人脸模型
    * */
    public FaceModel[] detectFacesModel(Image image, int maxFaceCount) {
        if (image != null) {
            BufferedImage bufferedImage = null;
            bufferedImage = Util.toBufferedImage(image);
            if (bufferedImage != null) {
                return detectFacesModel(bufferedImage, maxFaceCount);
            }
        }
        return null;

    }

    /*
    * 检测人脸
    *  maxFaceCount最多检测的人脸个数
    *  返回为人脸模型
    * */
    public FaceModel[] detectFacesModel(BufferedImage bufferedImage, int maxFaceCount) {
        if (bufferedImage != null) {
            return detectFacesModel(Util.getBgrDataFromBufferedImage(bufferedImage), bufferedImage.getWidth(),
                    bufferedImage.getHeight(), bufferedImage.getWidth() * 3, maxFaceCount);
        }
        return null;
    }

    /*
    * 检测人脸
    *  maxFaceCount最多检测的人脸个数
    *  imagePath人脸图片路径
    *  返回为人脸模型
    * */
    public FaceModel[] detectFacesModel(String imagePath, int maxFaceCount) throws IOException {
        if (imagePath != null) {
            return detectFacesModel(Util.toBufferedImage(imagePath), maxFaceCount);
        }
        return null;
    }

    /*
    * 检测人脸
    *  maxFaceCount最多检测的人脸个数
    *  imagePath人脸图片路径
    *  返回为人脸模型
    * */
    public FaceModel[] detectFacesModel(byte[] imgBgr24, int width, int height, int widthStep, int maxFaceCount) {
        return detectFaces(faceEngine, imgBgr24, width, height, widthStep, maxFaceCount);
    }

    /*
    * 根据人脸模型提取人脸的特征码
    * */
    public byte[] detectFaceFeature(Image image,FaceModel faceModel)
    {
        if (image != null) {
            BufferedImage bufferedImage = null;
            bufferedImage = Util.toBufferedImage(image);
            if (bufferedImage != null) {
                return detectFaceFeature(bufferedImage, faceModel);
            }
        }
        return null;
    }

    /*
    * 根据人脸模型提取人脸的特征码
    * */
    public byte[] detectFaceFeature(BufferedImage bufferedImage, FaceModel faceModel) {
        if (bufferedImage != null) {
            return detectFaceFeature(Util.getBgrDataFromBufferedImage(bufferedImage), bufferedImage.getWidth(),
                    bufferedImage.getHeight(), bufferedImage.getWidth() * 3, faceModel);
        }
        return null;
    }

    /*
    * 根据人脸模型提取人脸的特征码
    * */
    public byte[] detectFaceFeature(byte[] imgBgr24, int width, int height, int widthStep,FaceModel faceModel)
    {
        return  extractFeature(faceEngine,imgBgr24,width,height,widthStep,faceModel);
    }

    /*
    * 比对2张人脸的相似度
    * feature1 第一张人脸特征码
    * feature2 第二张人脸特征码
    * */
    public float feature2feature(byte[] feature1, byte[] feature2) {
        float degree = compare2Feature(faceEngine, feature1, feature2);
        return degree;
    }

    public float feature2featureV2(byte[] feature1, byte[] feature2) {
        float degree = compare2FeatureV2(faceEngine, feature1, feature2);
        return degree;
    }




    /*
     * 释放人脸引擎
     */
//    public void freeEngine() {
//        freeEngine(faceEngine);
//    }
}
